package com.ffcs.web.services;

import java.util.Date;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ffcs.web.model.dto.UserInfo;
import com.ffcs.web.model.entity.WxUser;
import com.ffcs.web.utils.CommonConfig;
import com.ffcs.web.utils.HttpClientSendPost;
import com.ffcs.web.utils.HttpGetUtil;
import com.ffcs.web.utils.JsonUtil;

@Service
public class CommonService {

	@Autowired
	private WxUserService wxUserService;
	
	private Logger logger = Logger.getLogger(CommonService.class);
	
	/**
	 * 获取openid
	 * @param code
	 * @return
	 */
	public WxUser getOpenId(String code){
		WxUser wxUser=null;
		String tempValue=bestfuzhouForm(code);
		JSONObject jsonObj = JSONObject.fromObject(tempValue);		
		String openId = "";
		String access_token = "";//获取用户信息的access_token
		String nickname = "";
		String refresh_token = "";
		String expires_in = "";
		
		if (jsonObj.containsKey("errcode")) {
			logger.info("获取openid出现异常");
			return null;
		}else{
			openId = jsonObj.getString("openid");
			access_token = jsonObj.getString("access_token");
			String userStatus=wxUserService.isAddUser(openId);
			if("0".equals(userStatus)){
				wxUser=saveUser(openId,access_token);
				return wxUser;
			}else{
				wxUser=wxUserService.getUser(openId);
				wxUser.setLastLoginTime(new Date());
				wxUserService.saveUser(wxUser);
			}
			
		}
		
		return wxUser;
	}
	
	/**
	 * 获取openid
	 * @param request
	 * @param response
	 * @param code
	 * @return
	 */
	public String bestfuzhouForm(String code){
		String appid = CommonConfig.getProperty("APPID");
		String appSecret = CommonConfig.getProperty("APPSECRET");		
		String oauthUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?&grant_type=authorization_code"; 
		String string = HttpGetUtil.doGet2(oauthUrl + "&code=" + code
				+ "&appid=" + appid + "&secret=" + appSecret,"");
//		String string = HttpClientSendPost.sendXMLDataByGet(oauthUrl + "&code=" + 123
//				+ "&appid=" + appid + "&secret=" + appSecret, "");
		return string;
	}
	
	public WxUser saveUser(String openId,String access_token){
		//获取用户信息
		String url="https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openId;
		logger.info("获得用户信息url="+url);
		String res = HttpGetUtil.doGet2(url,"");
		logger.info("获得用户信息"+res);
		UserInfo userInfo=(UserInfo)JsonUtil.jsonToBean(res, UserInfo.class);
		System.out.println(userInfo.getNickname());
		WxUser wxUser=null;
		try {
			wxUser=wxUserService.saveUser(userInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return wxUser;
	}
	
	public static void main(String[] args) {
		String appid = CommonConfig.getProperty("APPID");
		String appSecret = CommonConfig.getProperty("APPSECRET");		
		String oauthUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?&grant_type=authorization_code"; 
//		String string = HttpGetUtil.doGet2(oauthUrl + "&code=" + code
//				+ "&appid=" + appid + "&secret=" + appSecret,"");
		
		String res=HttpClientSendPost.sendXMLDataByGet(oauthUrl + "&code=" + 123
				+ "&appid=" + appid + "&secret=" + appSecret, "");
		System.out.println(res);
	}
}
